using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class TempArticleSalesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class TempArticleSalesDbo : DatabaseObject
        {
            public TempArticleSalesDbo()
            {
                Name = "TempArticleSales";
                GeneratedClassName = "TempArticleSales";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ArticleId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "Date" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Units", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Units" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Sales", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Sales" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "GM", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "GM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "GrossSales", ColumnType = "money", IsIdentity = false, IsNullable = false, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "GrossSales" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Price", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Price" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Confirmed", ColumnType = "bit", IsIdentity = false, IsNullable = true, Length = 1, Precision = 1, Scale = 0, GeneratedPropertyName = "Confirmed" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "NetMargin", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "NetMargin" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PIDiffUnits", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "PIDiffUnits" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PIDiff", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "PIDiff" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ChargeOffsUnits", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ChargeOffsUnits" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ChargeOffs", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "ChargeOffs" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Rebates", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Rebates" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "BonusBuyNumber", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "BonusBuyNumber" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Costs", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Costs" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CostsWithVat", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "CostsWithVat" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "RangeReviewId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CategoryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleCode", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 20, Precision = 0, Scale = 0, GeneratedPropertyName = "ArticleCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleName", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "ArticleName" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "EAN", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "EAN" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "UOM", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "UOM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Brand", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 255, Precision = 0, Scale = 0, GeneratedPropertyName = "Brand" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Manufacturer", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 255, Precision = 0, Scale = 0, GeneratedPropertyName = "Manufacturer" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "NotFoundCategory", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "NotFoundCategory" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Size", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Size" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Width", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Width" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Height", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Height" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Facings", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Facings" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute1", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute1" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute2", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute2" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute3", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute3" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute4", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute4" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute5", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute5" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TaxRate", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "TaxRate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CountryId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CountryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreCode", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreName", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreName" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Region", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Region" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "City", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "City" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Address", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 400, Precision = 0, Scale = 0, GeneratedPropertyName = "Address" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Phone", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Phone" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Fax", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Fax" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Format", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Format" });
              
            }
        }
		
		public static List<TempArticleSaleRow> Select(Expression<Func<TempArticleSaleRow, bool>> expression)
        {
            List<TempArticleSaleRow> rows = new List<TempArticleSaleRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<TempArticleSaleRow>(new TempArticleSalesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<TempArticleSaleRow> Select()
		{
			List<TempArticleSaleRow> rows = new List<TempArticleSaleRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<TempArticleSaleRow>(new TempArticleSalesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<TempArticleSaleRow> PagedSelect(Expression<Func<TempArticleSaleRow, bool>> filterExpression, Expression<Func<TempArticleSaleRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<TempArticleSaleRow> rows = new List<TempArticleSaleRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<TempArticleSaleRow>(new TempArticleSalesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<TempArticleSaleRow> TempArticleSaleList)
        {
            TempArticleSaleRow row;

            while (reader.Read())
            {
                row = new TempArticleSaleRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					StoreId = Util.CastDbValue<int?>(reader["StoreId"]),
					ArticleId = Util.CastDbValue<int?>(reader["ArticleId"]),
					Date = Util.CastDbValue<DateTime>(reader["Date"]),
					Units = Util.CastDbValue<int>(reader["Units"]),
					Sales = Util.CastDbValue<decimal>(reader["Sales"]),
					GM = Util.CastDbValue<decimal>(reader["GM"]),
					GrossSales = Util.CastDbValue<decimal>(reader["GrossSales"]),
					Price = Util.CastDbValue<decimal?>(reader["Price"]),
					Confirmed = Util.CastDbValue<bool?>(reader["Confirmed"]),
					NetMargin = Util.CastDbValue<decimal?>(reader["NetMargin"]),
					PIDiffUnits = Util.CastDbValue<int?>(reader["PIDiffUnits"]),
					PIDiff = Util.CastDbValue<decimal?>(reader["PIDiff"]),
					ChargeOffsUnits = Util.CastDbValue<int?>(reader["ChargeOffsUnits"]),
					ChargeOffs = Util.CastDbValue<decimal?>(reader["ChargeOffs"]),
					Rebates = Util.CastDbValue<decimal?>(reader["Rebates"]),
					BonusBuyNumber = Util.CastDbValue<string>(reader["BonusBuyNumber"]),
					Costs = Util.CastDbValue<decimal?>(reader["Costs"]),
					CostsWithVat = Util.CastDbValue<decimal?>(reader["CostsWithVat"]),
					RangeReviewId = Util.CastDbValue<int?>(reader["RangeReviewId"]),
					CategoryId = Util.CastDbValue<int?>(reader["CategoryId"]),
					ArticleCode = Util.CastDbValue<string>(reader["ArticleCode"]),
					ArticleName = Util.CastDbValue<string>(reader["ArticleName"]),
					EAN = Util.CastDbValue<string>(reader["EAN"]),
					UOM = Util.CastDbValue<string>(reader["UOM"]),
					Brand = Util.CastDbValue<string>(reader["Brand"]),
					Manufacturer = Util.CastDbValue<string>(reader["Manufacturer"]),
					NotFoundCategory = Util.CastDbValue<string>(reader["NotFoundCategory"]),
					Size = Util.CastDbValue<decimal?>(reader["Size"]),
					Width = Util.CastDbValue<decimal?>(reader["Width"]),
					Height = Util.CastDbValue<decimal?>(reader["Height"]),
					Facings = Util.CastDbValue<decimal?>(reader["Facings"]),
					Attribute1 = Util.CastDbValue<string>(reader["Attribute1"]),
					Attribute2 = Util.CastDbValue<string>(reader["Attribute2"]),
					Attribute3 = Util.CastDbValue<string>(reader["Attribute3"]),
					Attribute4 = Util.CastDbValue<string>(reader["Attribute4"]),
					Attribute5 = Util.CastDbValue<string>(reader["Attribute5"]),
					TaxRate = Util.CastDbValue<decimal?>(reader["TaxRate"]),
					CountryId = Util.CastDbValue<int?>(reader["CountryId"]),
					StoreCode = Util.CastDbValue<string>(reader["StoreCode"]),
					StoreName = Util.CastDbValue<string>(reader["StoreName"]),
					Region = Util.CastDbValue<string>(reader["Region"]),
					City = Util.CastDbValue<string>(reader["City"]),
					Address = Util.CastDbValue<string>(reader["Address"]),
					Phone = Util.CastDbValue<string>(reader["Phone"]),
					Fax = Util.CastDbValue<string>(reader["Fax"]),
					Format = Util.CastDbValue<string>(reader["Format"])
                };
                                
                TempArticleSaleList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(TempArticleSaleRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<TempArticleSaleRow>(new TempArticleSalesDbo(), row);
            }
        }

		public static void Update(TempArticleSaleRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<TempArticleSaleRow>(new TempArticleSalesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<TempArticleSaleRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<TempArticleSaleRow>(new TempArticleSalesDbo(), expression);
            }
        }

	}
}
	
